package com.maltys.dp.coin;

import java.util.Arrays;

/**
 * 518. 零钱兑换 II
 * 给你一个整数数组 coins 表示不同面额的硬币，另给一个整数 amount 表示总金额。
 * 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额，返回 0 。
 * 假设每一种面额的硬币有无限个。
 * 题目数据保证结果符合 32 位带符号整数。
 *
 * @author malty
 */
public class CoinChangeII {
    public static void main(String[] args) {
        CoinChangeII changeII = new CoinChangeII();
        int change = changeII.change(9, new int[]{1, 3, 5});
        System.out.println("=================");
        int change2 = changeII.change2(9, new int[]{1, 3, 5});
        System.out.println(change);
        System.out.println(change2);
    }

    public int change(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        System.out.println(Arrays.toString(dp));
        for (int i = 1; i <= coins.length; i++) {
            for (int j = coins[i - 1]; j <= amount; j++) {
                dp[j] += dp[j - coins[i - 1]];
            }
            System.out.println(Arrays.toString(dp));
        }
        return dp[amount];
    }

    public int change2(int amount, int[] coins) {
        int[][] dp = new int[coins.length][amount + 1];

        for (int i = 0; i <= amount; i++) {
            if (i % coins[0] == 0) {
                dp[0][i] = 1;
            }
        }
        for (int i = 0; i < coins.length; i++) {
            dp[i][0] = 1;
        }
        System.out.println(Arrays.toString(dp[0]));
        for (int i = 1; i < coins.length; i++) {
            for (int j = 1; j <= amount; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= coins[i]) {
                    dp[i][j] += dp[i][j - coins[i]];
                }
            }
            System.out.println(Arrays.toString(dp[i]));
        }
        return dp[coins.length - 1][amount];
    }
}
